// pages/cultureCoupons/couponsTopicPage/index.js
const common = require('../../../public.js')
const utils = require('../../../utils/utils.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityId: '',
    regionId: "",
    provinceId: '',
    imageId: '',
    tabList: [],
    benefitListData: [],
    isNoMoreData: false,
    tabActiveIndex: 0,
    from: 0,
    pageNum: 1,
    lon: '',
    lat: '',
    isLoadingBusy: false,
    storeId: '',
    hideCouponsTool: false,
    userInfo: null,
    realname: false,
    qrcodeid:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.q){
      let scan_url = decodeURIComponent(options.q);
      let video_id = scan_url.match(/\d+/) //提取链接中的数字，也就是链接中的参数id，/\d+/ 为正则表达式
       this.setData({
        regionId:video_id[0],
        imageId :video_id[0]
       })
       getApp().getLocation((data) => {
        this.data.lon = data.longitude
        this.data.lat = data.latitude
        this.getStoreList(true)
      }, () => {
        common.showToast('获取定位信息失败')
      })
      // this.getGoodsType()
      const cityName = this.getTopicTitle()
      if (cityName) {
        wx.setNavigationBarTitle({
          title: '第六届' + cityName + '惠民消费季',
        })
      }
      return
      }
    
    // 这里需要处理从实名页面返回调用onload方法没有参数的问题
      let imageId = ''
        this.setData({
          regionId:options.regionId,
          imageId :options.regionId
        })
       
    getApp().getLocation((data) => {
      this.data.lon = data.longitude
      this.data.lat = data.latitude
      this.getStoreList(true)
    }, () => {
      common.showToast('获取定位信息失败')
    })

    // this.getGoodsType()
    const cityName = this.getTopicTitle()
    if (cityName) {
      wx.setNavigationBarTitle({
        title: '第六届' + cityName + '惠民消费季',
      })
    }

  
  },

  onShow() {
    this.setData({
     
    })
    const that = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          realname: res.data.user.isRealname == 1
        })
      },
      fail: function () {
        that.setData({
          userInfo: null
        })
      }
    })
  },

  getStoreList(refresh = false) {
    if (this.data.isLoadingBusy || this.data.isNoMoreData) {
      return
    }
    this.setData({
      isLoadingBusy: true
    })
    if (refresh) {
      this.setData({
        benefitListData: []
      })
    }
    const pageNum = refresh ? 1 : this.data.pageNum + 1
    const param = {
      pageNum,
      pageSize: 10,
      xpoint: this.data.lon,
      ypoint: this.data.lat,
      cityId: this.data.cityId,
      regionId: this.data.regionId,
      provinceId: this.data.provinceId,
      storeType: this.data.tabActiveIndex === 0 ? '' : this.data.storeId
    }
    common.post('/store/getBenefitsPeopleStoreList2021', param, (res) => {
      if (res.status === 200) {
        this.data.pageNum = pageNum // 更新页面
        this.setData({
          benefitListData: refresh ? res.data : [...this.data.benefitListData, ...res.data]
        })
        this.setData({
          isNoMoreData: res.data.length < 10 ? true : false
        })
      } else {
        this.setData({
          isNoMoreData: true
        })
        common.showRequestErrMsg(res)
      }
    }, () => {
      this.setData({
        refreshing: false,
        isLoadingBusy: false
      })
    })
  },

  storeListClick(e) {
    common.gotoStore(e.currentTarget.dataset.item)
  },

  loadMore(e) {
    this.getStoreList(false)
  },
  getJumpParma() {
    let tmpList = []

    if (!common.isEmptyString(this.data.provinceId)) tmpList.push('provinceId=' + this.data.provinceId)
    if (!common.isEmptyString(this.data.cityId)) tmpList.push('cityId=' + this.data.cityId)
    if (!common.isEmptyString(this.data.regionId)) tmpList.push('regionId=' + this.data.regionId)

    return tmpList.join('&')
  },
  gotoCouponsCenter() {
    const url = '/pages/cultureCoupons/receiveCouponzc/index?' + this.getJumpParma()
    console.log(url)
    wx.navigateTo({
      url
    })
  },
  scan() {
    wx.scanCode({
      success: (res) => {
          const result = res.result
          console.log(result)
          //1. 扫码支付
          if (result && result.indexOf('/pay/scanForPay') != -1) {
              const quary = result.split('?')[1]
              wx.navigateTo({
                  url: '/pages/scanForPay/index?' + quary,
              })
          } else {
              common.showToast('暂不支持当前功能~')
          }
      },
      fail: (res) => {
          if (res.errMse != 'scanCode:fail cancel') {
              common.showToast('扫码失败')
          }
      }
  })
  },
  toRealName() {
    // if (this.data.cityId == 175) {
    //   wx.navigateTo({
    //     url: '/pages/cultureCoupons/discoverForCity/index?cityId=175',
    //   })
    // } else {
    //   wx.redirectTo({
    //     url: '/pages/discovery/index',
    //   })
    // }

    if (this.data.userInfo === null) {
      // 未登录
      common.showLoginPage()
    } else if (this.data.userInfo.isRealname != 1) {
      // 未实名
      common.toRealName()
    } else {
      common.showToast('您已实名')
    }
  },
  gotoCouponsList() {
    wx.navigateTo({
      url: '/pages/cultureCoupons/myCultureCoupons/index',
    })
  },
  closeCouponsTool() {
    this.setData({
      hideCouponsTool: true
    })
  },
  getTopicTitle() {
    return utils.getTopicTitle(!common.isEmptyString(this.data.provinceId) ? this.data.provinceId : this.data.cityId, this.data.regionId)
  },
  onShareAppMessage() {
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      path: '/pages/cultureCoupons/couponsTopicPage/index?' + this.getJumpParma()
    }
  },
  onShareTimeline() {
    const query = 'from=timeline&' + this.getJumpParma()
    return {
      title: '第六届' + this.getTopicTitle() + '惠民消费季',
      query
    }
  }
})